<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- <h2>
        组织架构
      </h2> -->
      <!-- <el-card>
        <el-row type="flex" justify="space-between">
          <el-col>
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>

          <el-col :span="4">
            <el-row type="flex" class="row-bg" justify="end">
              <el-col>负责人</el-col>
              <el-col>
                <el-dropdown>
                  <span class="el-dropdown-link">
                    操作<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>黄金糕</el-dropdown-item>
                    <el-dropdown-item>狮子头</el-dropdown-item>
                    <el-dropdown-item>螺蛳粉</el-dropdown-item>
                    <el-dropdown-item disabled>双皮奶</el-dropdown-item>
                    <el-dropdown-item divided>蚵仔煎</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

      </el-card> -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col>
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end"> -->
        <!-- 两个内容 -->
        <!-- <el-col>负责人</el-col>
              <el-col> -->
        <!-- 下拉菜单 element -->
        <!-- <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span> -->
        <!-- 下拉菜单 -->
        <!-- <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row> -->
        <treeTools :tree-node="company" :is-root="true" @addDepartment="addDepartment" />
        <hr>

        <el-tree :data="depts" :props="{label:'name'}" default-expand-all>
          <!-- <template slot-scope="obj"> -->
          <!-- <template #default="obj"> -->
          <template v-slot="obj">
            <treeTools
              :tree-node="obj.data"
              @delFn="getDepartment"
              @addDepartment="addDepartment"
              @editDepartment="editDepartment"
            />
            <!-- {{ obj.data.name }} -->
            <!-- <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col>
                <span>{{ obj.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end"> -->
            <!-- 两个内容 -->
            <!-- <el-col>{{ obj.data.message }}</el-col>
                  <el-col> -->
            <!-- 下拉菜单 element -->
            <!-- <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span> -->
            <!-- 下拉菜单 -->
            <!-- <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row> -->
          </template>
        </el-tree>
      </el-card>
      <!-- <add-dept :is-show-dialog="isShowDialog" /> -->
      <add-dept ref="addFrom" :is-show-dialog.sync="isShowDialog" :tree-node="treeNode" @addDepartment="getDepartment" />
    </div>
  </div>

</template>

<script>
import treeTools from '@/components/tree-tools'
import { getDepartment } from '@/api/department'
import { listToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'

export default {
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      // isShow: { isShowDialog: false },
      isShowDialog: false,
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      depts: [],
      treeNode: {}

    }
  },
  async created() {
    // const { depts } = await getDepartment()
    // // console.log(depts)
    // this.depts = tranListToTreeData(depts, '')
    this.getDepartment()
  },
  methods: {
    initFn() {
      console.log(1)
      this.getDepartment()
      this.$message.success('新增子部门成功')
    },
    async getDepartment() {
      const { depts } = await getDepartment()
      // console.log(depts)

      // 改造depts数组
      this.depts = listToTreeData(depts, '')
    },
    // 新增部门
    addDepartment(val) {
      // this.isShowDialog = true
      this.isShowDialog = true
      this.treeNode = val
    },
    // 编辑部门
    async editDepartment(val) {
      this.treeNode = val
      await this.$refs.addFrom.getDepar(val.id)
      // this.$refs.addFrom.fromInit()
      this.isShowDialog = true
    }
  }

}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
